package com.mars.basic.client.controller;

import com.mars.basic.allmain.dto.RoleDto;
import com.mars.basic.allmain.entity.RoleEntity;
import com.mars.basic.allmain.po.Role;
import com.mars.basic.allmain.po.QRole;
import com.mars.basic.client.IRoleClient;
import com.mars.basic.factory.RoleFac;
import com.mars.basic.service.RoleService;
import com.mars.common.utils.AnyWhereConditionPath;
import com.mars.common.utils.Beans;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class RoleController implements IRoleClient {
    @Autowired
    private RoleService service;
    @Override
    public Role addRole(@RequestBody RoleDto dto) {
        RoleEntity role = Beans.mapper(dto, RoleEntity.class);
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        service.getRoleRepository().save(role);
        RoleEntity one = service.getRoleRepository().findOneIfMutil(QRole.role.name.eq(role.getName()));
        return one;
    }

    @Override
    public List<RoleDto> getList() {
        BooleanExpression where = AnyWhereConditionPath.anyWherePredicate();
        List<RoleEntity> all = service.getRoleRepository().findAll(where);
        return all.stream().map(RoleFac::toDto).collect(Collectors.toList());
    }
}
